package com.zhongnan.demo7.executors;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MyThreadPool1 {
    public static void main(String[] args) {
        // 1.核心线程的数量
        // 2.最大线程数量
        // 3.临时线程的存活时间（值）
        // 4.临时线程的存活时间（单位）
        // 5.任务队列
        // 6.创建线程工厂
        // 7.任务的拒绝策略
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                2,
                4,
                2,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(8),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        // new ThreadPoolExecutor.AbortPolicy() 抛弃任务并抛出异常
        // new ThreadPoolExecutor.DiscardPolicy() 抛弃任务不抛出异常
//        new ThreadPoolExecutor.DiscardOldestPolicy() 抛弃等待最久的任务，将新任务加入进去
//        new ThreadPoolExecutor.CallerRunsPolicy() 调用任务的run方法绕过线程池直接执行
        pool.getPoolSize();
        for (int i = 0; i < 13; i++) {

            pool.submit(() -> {
                System.out.println(Thread.currentThread().getName() + "在执行");
            });
        }
        pool.shutdown();
    }
}
